package cn.nexd.app.indoor.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.nexd.app.indoor.R;
import cn.nexd.app.indoor.application.MyApplication;

/**
 * Settings activity to config the param of indoor location
 *
 * Created by wangxu on 16/7/20.
 */
public class SettingsActivity extends Activity {

    @Bind(R.id.location_time)
    EditText locationTime;

    @Bind(R.id.smooth)
    SwitchCompat smooth;

    @Bind(R.id.commit)
    Button commit;

    @Bind(R.id.top_layout_title)
    TextView tvTopTitle;

    @Bind(R.id.top_layout_back)
    ImageView ivTopBack;

    @Bind(R.id.auto_change_floor)
    SwitchCompat autoChangeFloor;

    @Bind(R.id.wifi_location_model)
    SwitchCompat wifiLocationMode;

    @Bind(R.id.beacon_location_mode)
    SwitchCompat beaconLocationMode;

    @Bind(R.id.mix_location_mode)
    SwitchCompat mixLocationMode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_layout);
        ButterKnife.bind(this);
        tvTopTitle.setText("配置");
        ivTopBack.setVisibility(View.GONE);
        initView();
    }

    private void initView() {
        locationTime.setText(String.valueOf(MyApplication.LOCATION_TIME));
        smooth.setChecked(MyApplication.IS_SMOOTH_OUTPUT);
        autoChangeFloor.setChecked(MyApplication.AUTO_CHANGE_FLOOR);
        if (MyApplication.IS_WIFI && MyApplication.IS_BEACON) {
            mixLocationMode.setChecked(true);
            wifiLocationMode.setChecked(false);
            beaconLocationMode.setChecked(false);
        } else {
            mixLocationMode.setChecked(false);
            wifiLocationMode.setChecked(MyApplication.IS_WIFI);
            beaconLocationMode.setChecked(MyApplication.IS_BEACON);
        }
    }

    @OnClick(R.id.commit)
    public void onClick() {
        // 定位间隔, 默认1s,
        MyApplication.LOCATION_TIME = Integer.parseInt(locationTime.getText().toString().trim());
        // 是否平滑输出
        MyApplication.IS_SMOOTH_OUTPUT = smooth.isChecked();
        // 自动切层
        MyApplication.AUTO_CHANGE_FLOOR = autoChangeFloor.isChecked();
        // Mix模式定位
        if (mixLocationMode.isChecked()) {
            // 设置Wi-Fi 蓝牙都为false, 即便页面修改了,也不设置
            MyApplication.IS_WIFI = true;
            MyApplication.IS_BEACON = true;
        } else {
            // 非混合定位, 该是什么就是什么, 但是只能选择一个
            if (beaconLocationMode.isChecked() == wifiLocationMode.isChecked()) {
                Toast.makeText(SettingsActivity.this, "非混合定位条件下,Wi-Fi和蓝牙只能选择一个,且必须选择一个!", Toast.LENGTH_SHORT).show();
                return;
            }
            MyApplication.IS_BEACON = beaconLocationMode.isChecked();
            MyApplication.IS_WIFI = wifiLocationMode.isChecked();
        }

        this.finish();
    }
}
